clear
clear all
cap log close

log using "log-files/3_Table_E1.smcl", replace smcl

timer on 2

eststo clear

*-----------*	
* Table E.1 *
*-----------*
	* Our regressions
	* Loop over dates
	foreach date in ulttranstrepdate_missingsatmunmedian verificadortrepdate_missingsatthend {
		* Use data set with problem booths appended at the end
		use "data/outputs/computo_`date'.dta", clear
			
		* Capture cutoff (shutdown) percentile		
		qui su pcs_nbnn if trep_date >= tc(20oct2019 19:40:57) & ///
			trep_date <= tc(20oct2019 19:40:58), detail
		local cutoff = `r(min)' 
			
		* Post dummies
		gen pcs_centered = pcs_nbnn - `cutoff'
		gen post = pcs_nbnn >= `cutoff'
		gen postXpcs = post*pcs_centered	   
		gen margin_nbnn = mshare_nbnn - cshare_nbnn // Excluding blancos y nulos
		
		* Recinto ID
		egen reci_id = group(Pais Dep Prov Muni Loc Reci)
		tostring reci_id, replace
		codebook reci_id
		replace reci_id=reci_id+"-1" if Dep=="La Paz" & Prov=="Murillo" & ///
			Muni=="Nuestra Señora de La Paz" & Reci=="Unidad Educativa Privada La Paz" ///
			& NumMesa>=22383 & NumMesa<=22386
		replace reci_id=reci_id+"-2" if Dep=="La Paz" & Prov=="Murillo" & ///
			Muni=="Nuestra Señora de La Paz" & Reci=="Unidad Educativa Privada La Paz" ///
			& NumMesa>22386
		codebook reci_id

		* Labels
		label var pcs_nbnn "Reporting time percentile"
		label var post "Post shutdown"

		* Our regression
		eststo: reghdfe margin_nbnn post, a(reci_id) cl(reci_id)
		eststo: reghdfe margin_nbnn pcs_nbnn post, a(reci_id) cl(reci_id)
	}

	* Adding FR data
	* verificador date
	use "data/outputs/computo_verificadortrepdate_missingsatthend.dta", clear

	* Capture cutoff (shutdown) percentile
	qui su pcs_nbnn if trep_date >= tc(20oct2019 19:40:57) & ///
		trep_date <= tc(20oct2019 19:40:58), d
	local cutoff = `r(min)' 

	* Recinto ID
	egen reci_id = group(Pais Dep Prov Muni Loc Reci)
		 /* Note, in constructing the data set we made a correction
			such that recinto names are unique within localities. */

	gen margin_nbnn = mshare_nbnn - cshare_nbnn // Excluding blancos y nulos

	* Adding website time stamps
	sort NumMesa
	merge 1:1 NumMesa using "data/inputs/1_Bolivia_InEarly.dta"
	drop _merge
	sort NumMesa

	merge 1:1 NumMesa using "data/inputs/1_Bolivia_VotosValidos.dta"

	gen post = 1 - inEarly

	gen sMAS = 100 * MAS / VotosV
	gen sCC = 100 * CC / VotosV
	gen margin = (sMAS - sCC) / 100

	* Labels
	label var pcs_nbnn "Reporting time percentile"
	label var post "Post shutdown"

	* Escobari-Hoover regression
	eststo: reghdfe margin post, a(reci_id) cl(reci_id)

	* Using our margin (valid votes instead of VotosValidos)
	eststo: reghdfe margin_nbnn post, a(reci_id) cl(reci_id)

	* Fragment			
	esttab using "outputs/generated/Table_E1.tex", ///
		b(a2) replace alignment(S) substitute(/_ _) ///
		keep(post pcs_nbnn_so) ///
		gaps compress se nostar ///
		bookt fragment label nomtitles ///
		nodepvars nonumbers nolines obslast ///
		cells(b(fmt(4)) se(fmt(a2) par))

	* Additional formatting
	* Insheet results
	insheet using "outputs/generated/Table_E1.tex", clear

	* Delete first line
	drop in 1 / 1

	* Spacing and font size
	replace v1 = "\addlinespace[0.5ex]" if regexm(v1, "addlinespace")
	replace v1 = "\rowfont{\scriptsize} " + v1 if regexm(v1, "\)&") 
	replace v1 = v1 + "[-.25em]" if regexm(v1, "\)&") == 0 & ///
		regexm(v1, "addlinespace") == 0

	* Label
	replace v1 = subinstr(v1, "Post shutdown", "Post shutdown (0/1)", .)

	* Obs formatting
	replace v1 = subinstr(v1, "32", "32,", .) if regexm(v1, "Observations")
	replace v1 = "\midrule" if regexm(v1[_n + 1], "Observations")

	* Outsheet
	outsheet using "outputs/generated/Table_E1.tex", ///
		nonames noquote replace

*------------------------------------------------------------------------------*
timer off 2
timer list 2

log close
clear all
